//
//  MainViewController.swift
//  myWeibo
//
//  Created by MrWu on 16/4/7.
//  Copyright © 2016年 MrWu. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {
   
    @objc private func clickComposedButton() {
        print("点击了中间按钮")
    }
    override func viewDidLoad() {
        
        super.viewDidLoad()
     
        setupChildrenControllers()
        setupComposedButton()
        
    }
    
    override func viewWillAppear(animated: Bool) {
        super.viewWillAppear(animated)
        tabBar.addSubview(composedButton)

    }
    
    private lazy var composedButton:Extension = Extension(imageNamed: "tabbar_compose_icon_add", backImageNamed: "tabbar_compose_button")
}


extension MainViewController {
    
    func setupComposedButton() {
        
        let count = childViewControllers.count
        
        let w = tabBar.bounds.width / CGFloat(count) - 1
        
        composedButton.frame = CGRectInset(tabBar.bounds, w * 2, 0)
        
        composedButton.addTarget(self, action: "clickComposedButton", forControlEvents: UIControlEvents.TouchUpInside)
    }
    
    func setupChildrenControllers() {
        tabBar.tintColor = UIColor.orangeColor()
        
        addChildViewController(HomeViewController(), title: "首页", imageNamed: "tabbar_home")
        addChildViewController(MessageViewController(), title: "消息", imageNamed: "tabbar_message_center")
        addChildViewController(UIViewController())
        addChildViewController(DiscoverViewController(), title: "发现", imageNamed: "tabbar_discover")
        addChildViewController(ProfileViewController(), title: "我", imageNamed: "tabbar_profile")

    }
    
    
    func addChildViewController(vc: UIViewController,title:String,imageNamed:String) {
        
        vc.title = title
        
        vc.tabBarItem.image = UIImage(named: imageNamed)
        
        addChildViewController(UINavigationController(rootViewController: vc))
        
        
    }
    
    
}